/**
 * 常用的正则表达式
 */

export const pwdExp = {
	exp: /^[0-9a-zA-Z-_%`~!@#\u0024\u005E&\u002A\u0028\u0029\u002B=]{6,18}$/,
	des: '6-18个字符（只能包含英文字母、数字、特殊符号，不能包含空格）'
};
export const mobileExp = {
	exp: /^1[3456789]\d{9}$/,
	des: '请输入正确的手机号'
};

export const nameExp = {
	exp: /^[\u4e00-\u9fa5a-zA-Z0-9]{1,40}$/,
	des: '需要1-40个字符，只能输入中文、数字或英文字母'
};

export const numberExp = {
	exp: /^[0-9]+([.]\d{1,2})?$/,
	des: '请输入合法的正数,最多两位小数'
};

export const macExp = {
	exp: /^[a-zA-Z0-9]+$/,
	des: 'mac只能输入字母与数字'
};

export const expObj = {
	pwd: pwdExp,
	mobile: mobileExp,
	name: nameExp,
	number: numberExp,
	mac: macExp
};

export function patternObj(key) {
	const exp = expObj[key];
	return { pattern: exp.exp, message: exp.des };
}

export default function validateFun(key) {
	let obj = key;
	if (typeof key === 'string') obj = expObj[key];
	return text => {
		return obj.exp.test(text) ? null : obj.des;
	};
}
